

 %
 %
 %% Persistence and Dispersion of ANNUAL APL 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 %
 % First, need annual data. 
 % APL is total output over year / mid-year employment. 
 % Use of point-in-time measure of Employment is
 % consistent with how it is typically measured
 % in plant-level surveys.
yrs = months/12;
TotOutput12mo = zeros(NumFirms, yrs); 
EmpGrowth12mo = zeros(NumFirms, yrs);   % This will be used later in the program.
MidptEmp12mo  = zeros(NumFirms, yrs);
for j = 1:yrs
    j1= 12*(j-1) + 1;
    j2= 12*j; 
    midpt = (j2+j1-1)/2;
    EmpGrowth12mo(:,j)  = (EmpMatmo(:, j2+1) - EmpMatmo(:,j1)) ./ (0.5*(EmpMatmo(:, j2+1) + EmpMatmo(:,j1)));
    TotOutput12mo(:,j)  = sum( Output1mo( :, j1:j2), 2);    % annual output
    MidptEmp12mo(:,j)   = EmpEndofmo(:, midpt);
end
AvgProd12mo = TotOutput12mo ./ MidptEmp12mo;


 %
 %
 % Autocorrelation of APL
yrobs = yrs  -1;    % will lose a year to compute autocorrelation.
EmpWght12mo = 0.5*(MidptEmp12mo(:, 2:end) + MidptEmp12mo(:, 1:end-1));	% Average size used in weighted OLS (see below)
EmpWght12mo = reshape( EmpWght12mo, NumFirms*yrobs, 1);         
EmpShr      = EmpWght12mo / sum(EmpWght12mo);
 %
X = [reshape( log(AvgProd12mo(:, 1 :end-1)), NumFirms*yrobs, 1), ones(NumFirms*yrobs, 1)];
Y =  reshape( log(AvgProd12mo(:, 2 :end  )), NumFirms*yrobs, 1);
ro = (1:1:length(Y))';
co = ro;    
WW = sparse(ro,co,EmpShr,length(Y),length(Y));
coef = (X'*WW*X)\(X'*WW*Y);
rho_logApl = coef(1);


 %
 %
 % Dispersion of log APL
logApl    = reshape( log(AvgProd12mo), NumFirms*yrs, 1 );
EmpWght   = reshape( MidptEmp12mo, NumFirms*yrs, 1 );   % weighted by average size. 
AplWghts  = EmpWght'/sum(EmpWght);
ElogApl   = AplWghts * logApl;   
var_logApl= AplWghts * ((logApl - ElogApl).^2);




 %
 %
 % Distribution of APL GROWTH.
 % This computes dispersion for inactive and active firms. 
 % The results are compared to compustat estimates (Appendix).
ybin = (-1.99 :.01: 1.99)';
ybins= length(ybin) + 1;
midybin = .5*( (-2 :.01: 1.99)' + (-1.99 :.01: 2)' );
 %
APLGrowth12mo   = (AvgProd12mo( :, 2:end) - AvgProd12mo( :, 1:end-1)) ./ (0.5*(AvgProd12mo( :, 2:end) + AvgProd12mo( :, 1:end-1))); 
AplGrowth       = zeros(ybins, 1);  % for all employers
aplgrowth       = zeros(ybins, 1);
InactAplGrowth  = zeros(ybins, 1);  % for employment inactive employers
inactaplgrowth  = zeros(ybins, 1);
AdjAplGrowth    = zeros(ybins, 1);  % for employment adjusting employers
adjaplgrowth    = zeros(ybins, 1);
InactFirmYrs = 0;
AdjFirmYrs   = 0;
%
for j = 1:yrs-1
    GrossGrowth = EmpGrowth12mo(:,j+1);     % first observation is growth in year 2.
    einactive   = find(GrossGrowth==0);
    eactive     = find(GrossGrowth >0 | GrossGrowth <0);
    inactfirmyrs = length(einactive);
    adjfirmyrs   = length(eactive);
    %
    for k = 1:ybins
        %
        % All:
        %%%%%%%
        if k==1
            ros = find(APLGrowth12mo(:,j) <ybin(1));
        elseif k ==ybins
            ros = find(APLGrowth12mo(:,j) >ybin(ybins-1));
        else
            ros = find(APLGrowth12mo(:,j)>=ybin(k-1) & APLGrowth12mo(:,j) <ybin(k));
        end
        if isempty(ros) ==0
            AplGrowth(k) = aplgrowth(k) + length(ros);
        end
        %
        % Inactive:
        %%%%%%%%%%%
        if k==1
            ros = find(APLGrowth12mo(einactive,j) <ybin(1));
        elseif k ==ybins
            ros = find(APLGrowth12mo(einactive,j) >ybin(ybins-1));
        else
            ros = find(APLGrowth12mo(einactive,j)>=ybin(k-1) & APLGrowth12mo(einactive,j) <ybin(k));
        end
        if isempty(ros) ==0
            InactAplGrowth(k) = inactaplgrowth(k) + length(ros);
        end
        %
        % Active:
        %%%%%%%%%%%
        if k==1
            ros = find(APLGrowth12mo(eactive,j) <ybin(1));
        elseif k ==ybins
            ros = find(APLGrowth12mo(eactive,j) >ybin(ybins-1));
        else
            ros = find(APLGrowth12mo(eactive,j)>=ybin(k-1) & APLGrowth12mo(eactive,j) <ybin(k));
        end
        if isempty(ros) ==0
            AdjAplGrowth(k) = adjaplgrowth(k) + length(ros);
        end
        
    end
    aplgrowth = AplGrowth;
    adjaplgrowth = AdjAplGrowth;
    inactaplgrowth = InactAplGrowth;
    InactFirmYrs = InactFirmYrs + inactfirmyrs;
    AdjFirmYrs   = AdjFirmYrs + adjfirmyrs;
end
 %
APLGrowthDist12mo    = AplGrowth / (NumFirms*yrobs);
InAPLGrowthDist12mo  = InactAplGrowth / InactFirmYrs;
AdjAPLGrowthDist12mo = AdjAplGrowth / AdjFirmYrs;
aplgrowthMAT     = [midybin, APLGrowthDist12mo, InAPLGrowthDist12mo, AdjAPLGrowthDist12mo];
 %
EAPLGrowth12mo   = APLGrowthDist12mo'*midybin;
EInAPLGrowth12mo = InAPLGrowthDist12mo'*midybin;
EAdjAPLGrowth12mo= AdjAPLGrowthDist12mo'*midybin;
VAPLGrowth12mo   = APLGrowthDist12mo'*( (midybin-EAPLGrowth12mo).^2 );
VInAPLGrowth12mo = InAPLGrowthDist12mo'*( (midybin-EInAPLGrowth12mo).^2 );
VAdjAPLGrowth12mo= AdjAPLGrowthDist12mo'*( (midybin-EAdjAPLGrowth12mo).^2 );
